* This file generates the results in the last two columns of Table 4
* The market reaction to repurchase announcements

clear all

* Set your directory path here
local root "D:\Understanding_Bank_Payouts"

capture log close
log using "`root'\Code\Logs\Final_code_RCFS\Table4_repurchases.log", replace
display "$S_TIME  $S_DATE"

capture noisily mkdir "`root'\Results"
capture noisily mkdir "`root'\Results\Final_code_RCFS"
cd "`root'\Results\Final_code_RCFS"

local finalfile_yearly "`root'\Data\Final\Finalfile_yearly.dta" 

use "`finalfile_yearly'" 
keep permno instkey
dups, t drop
drop _expand
drop if permno == .
save TEMPpermno_instkey.dta, replace

// Cumulative abnormal returns for all repurchase events
use  "`root'\Data\Repurchase_eventstudy\CARs_allfirms.dta", clear
drop if _merge == 1					
drop _merge
merge m:1 permno using TEMPpermno_instkey.dta, keep(1 3) nogenerate
erase TEMPpermno_instkey.dta

gen quarter = qofd(date)
format quarter %tq
gen year = year(date)

merge m:1 instkey quarter using "`root'\Data\Divdeclare_eventstudy\SNL_EAdates_Peter.dta"
merge m:1 instkey quarter using "`root'\Data\Final\Finalfile_quarterly.dta", keepusing(netprofit) keep(1 3) nogenerate

gen eagap0 = date - eadate

foreach var of varlist car03_FF4 carm3p3_FF4 carm5p5_FF4 scar03_FF4 scarm3p3_FF4 scarm5p5_FF4 {
	winsor `var', generate(helper) p(0.01)
	drop `var'
	rename helper `var'
	
	sum `var'
	sum `var' if eventtype >= 1 & eventtype <= 3
	sum `var' if (eventtype == 1 | eventtype == 3  | eventtype == 4)
	ttest `var' == 0 if eventtype >= 1 & eventtype <= 3
	ttest `var' == 0 if (eventtype == 1 | eventtype == 3  | eventtype == 4)
	tabstat `var' if eventtype >= 1 & eventtype <= 3, by(year) stats(mean N)
	tabstat `var' if (eventtype == 1 | eventtype == 3  | eventtype == 4), by(year) stats(mean N)
	}


foreach var in car03_FF4 carm3p3_FF4 carm5p5_FF4 {
	
	file open CAR_results using "Table4_repurchase_eventstudy_`var'.txt", write replace
	file write CAR_results "The market reaction (`var') to repurchase announcements" _n(2)

	***************************************************************************
	* Drop all repurchase announcements that are on the same day as an earnings announcement
	***************************************************************************
	drop if abs(eagap0) < 1	

	file write CAR_results "Reaction to repurchase announcements [authorization dates] - excluding those that were announced together with earnings" _n(2)
	file write CAR_results "Year" _tab "Average CAR" _tab "N" _tab "t statistic" _tab ///
	"p value" _tab "asterisks"  _n

	file write CAR_results _n(2)	

	preserve
	keep if year <= 2006

	summarize `var' if year >= 1995 & year <=2008 & (eventtype == 1 | eventtype == 3  | eventtype == 4) 
	file write CAR_results "1995-2006" _tab (r(mean)) _tab (r(N)) _tab	// Show the descriptives on the CAR, and use the B�hmer et al. stat for the t-test
	ttest s`var' == 0 if year >= 1995 & year <=2008 & (eventtype == 1 | eventtype == 3  | eventtype == 4) 
	file write CAR_results (r(t)) _tab (r(p)) 
	local pvalue = r(p)
	if r(p) < 0.01 {
		file write CAR_results "***" _n
		}
	else {
		if r(p) < 0.05 {
				file write CAR_results "**" _n
				}
		else {
			if r(p) < 0.1 {
				file write CAR_results "*" _n
				}	
			else {
				file write CAR_results _n
				}		
			}
		}	
		
	restore, preserve

	keep if year == 2007 | year == 2008

	summarize `var' if year >= 1995 & year <=2008 & (eventtype == 1 | eventtype == 3  | eventtype == 4) 
	file write CAR_results "2007-2008" _tab (r(mean)) _tab (r(N)) _tab	// Show the descriptives on the CAR, and use the B�hmer et al. stat for the t-test
	ttest s`var' == 0 if year >= 1995 & year <=2008 & (eventtype == 1 | eventtype == 3  | eventtype == 4) 
	file write CAR_results (r(t)) _tab (r(p)) 
	local pvalue = r(p)
	if r(p) < 0.01 {
		file write CAR_results "***" _n
		}
	else {
		if r(p) < 0.05 {
				file write CAR_results "**" _n
				}
		else {
			if r(p) < 0.1 {
				file write CAR_results "*" _n
				}	
			else {
				file write CAR_results _n
				}		
			}
		}

	restore
		
	** Full sample

	summarize `var' if year >= 1995 & year <=2008 & (eventtype == 1 | eventtype == 3  | eventtype == 4)
	file write CAR_results "Full sample" _tab (r(mean)) _tab (r(N)) _tab	// Show the descriptives on the CAR, and use the B�hmer et al. stat for the t-test
	ttest s`var' == 0 if year >= 1995 & year <=2008 & (eventtype == 1 | eventtype == 3  | eventtype == 4)
	file write CAR_results (r(t)) _tab (r(p)) 
	local pvalue = r(p)
	if r(p) < 0.01 {
		file write CAR_results "***" _n
		}
	else {
		if r(p) < 0.05 {
				file write CAR_results "**" _n
				}
		else {
			if r(p) < 0.1 {
				file write CAR_results "*" _n
				}	
			else {
				file write CAR_results _n
				}		
			}
		}	

	file close CAR_results

	}	
	

display "$S_TIME  $S_DATE"
log close
clear
exit
